<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Auth;
use App\Novel;
use App\User;
use App\Mobile;
use App\Chapter;
use App\Comment;
use App\Tool\SMS\SendTemplateSMS;
class IndexController extends Controller
{
    public function index(){
      $novels_0 = Novel::orderby('clickcount','desc')->take(4)->get();  //放到轮播图
    	$novels_1 = Novel::orderby('nid','desc')->take(4)->get();    //取出小说信息
    	$novels_2 = Novel::orderby('nid','desc')->skip(4)->take(4)->get();    //取出小说信息
      $novels_3 = Novel::orderby('collection','desc')->take(3)->get();    //放到收藏榜
      $novels_4 = Novel::where(['status'=>0,'gender'=>0])->orderby('nid','desc')->take(4)->get();//放到女频道推荐
      $novels_5 = Novel::where(['status'=>0,'gender'=>1])->orderby('nid','desc')->take(4)->get();//放到男频道推荐
      $novels_6 = DB::table('novels')->join('chapters','novels.new','=','chapters.chid')->select('novels.*','chapters.chtitle')->orderby('novels.edittime')->get();
    	return view('index', ['novels_0'=>$novels_0,'novels_1'=>$novels_1, 'novels_2'=>$novels_2,'novels_3'=>$novels_3,'novels_4'=>$novels_4,'novels_5'=>$novels_5,'novels_6'=>$novels_6]);  //把数据传到index页面
    }


    public function getHomepage(Request $req, $nid){
      $novel = Novel::find($nid);
      $novel->clickcount += 1;
      $novel->save();
      $uid = $novel->uid;
      $user = User::find($uid); //获取作者信息
      $desc = explode('   ',$novel->desc);
      // dd($desc);
      $chapter = Chapter::where('nid',$nid)->orderby('chid','desc')->take(4)->get(); //展示部分目录
      $comment = Comment::where('nid',$nid)->orderby('cid','desc')->get();  //展示查询评论
      $last = Chapter::where('nid',$nid)->orderby('chid','desc')->first(); // 获得最新更新的章节
      $first = Chapter::where('nid',$nid)->orderby('chid')->first(); //获得第一章节的内容
      $head = DB::table('comments')->join('users', 'comments.uid', '=', 'users.uid')->where('nid',$nid)->orderby('cid','desc')->paginate(2);


    	return view('public/homepage',['head' =>$head ,'desc' =>$desc ,'novel' => $novel,'chapter' => $chapter,'comment' => $comment,'user' =>$user,'last' => $last,'first'=>$first]);
    }

    public function postHomepage(Request $req , $nid){
      $com = new Comment();
      $com->nid = $nid;
      $user = Auth::user();
      $com->uid = $user->uid;
      $com->name = $user->name;
      $com->title = $req->all()['content'];
      $com->pubtime = time();
      $com->save();
      return redirect("homepage/$nid");
    }
     //手机短信验证码
    public function haha(Request $req){
      $shoujihao = $_GET['name'];
      //$shoujihao = $req->input('mobile','');
      //dd($shoujihao);
      $a = new sendTemplateSMS();
      $code = mt_rand(100000,999999);
      $a->sendTemplateSMS($shoujihao,array($code,'5'),"1");
      $mobile = new Mobile;
      $mobile->mobile = $shoujihao;
      $mobile->token = $code;
      $mobile->pubtime = time();
      $mobile->save();
}


    public function chapterlist($nid){
      $novel = Novel::find($nid);                           //获取小说信息
      $chapters = Chapter::where('nid', $nid)->get();       //获取章节信息

      return view('public.chapter-list',['novel'=>$novel,'chapters'=>$chapters]);
    }

}
